package cn.school.hotel.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.school.hotel.dao.HotelManageDao;
import cn.school.hotel.dao.TripHotelRoomDao;
import cn.school.hotel.dao.TripHotelTempStoreDao;
import cn.school.hotel.dao.TripProductStoreDao;
import cn.school.hotel.entity.*;
import cn.school.hotel.service.HotelManageService;
import cn.school.hotel.vo.ItripHotelRoomVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service("hotelManageService")
public class HotelManageServiceImpl extends ServiceImpl<HotelManageDao, TripHotel> implements HotelManageService {
    @Autowired
    private HotelManageDao hotelManageDao;

    @Autowired
    private TripHotelRoomDao tripHotelRoomDao;

    @Autowired
    private TripProductStoreDao tripProductStoreDao;

    @Autowired
    private TripHotelTempStoreDao tripHotelTempStoreDao;

    @Override
    public IPage<TripHotel> getHotelPagelist(TripHotel tripHotel, Integer pageSize, Integer pageNum) {
        LambdaQueryWrapper<TripHotel> wrapper = new LambdaQueryWrapper<>(tripHotel);
        if (StrUtil.isNotEmpty(tripHotel.getHotelName())) {
            wrapper.eq(TripHotel::getHotelName, tripHotel.getHotelName());
        }
        wrapper.orderByDesc(TripHotel::getCreationDate);

        return  page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public int createHoteInfo(TripHotel tripHotel) {
        int count = 0;
        if(StringUtils.isEmpty(tripHotel.getId()) || "0".equals(tripHotel.getId())){
            tripHotel.setCreationDate(new Date());
            count = hotelManageDao.insert(tripHotel);
        }else{
            tripHotel.setModifyDate(new Date());
            count = hotelManageDao.updateById(tripHotel);
        }

        return count;
    }

    @Override
    public TripHotel getHotelInfoDetail(String id) {
        return hotelManageDao.selectById(id);
    }

    @Override
    public int deleteHoteInfo(String id) {
        return hotelManageDao.deleteById(id);
    }

    @Override
    public List<ItripHotelRoomVO> getHotelRoomList(String id) {
        return tripHotelRoomDao.getHotelRoomList(id);
    }

    @Override
    public int createOrUpdateHotelRoom(List<TripHotelRoom> roomList) {
        int num = 0 ;
        if(roomList != null){
            for(TripHotelRoom room : roomList) {
                if("1".equals(room.getIsDelete()) && !StringUtils.isEmpty(room.getId())){
                    tripHotelRoomDao.deleteById(room);//删除房间
                    //删除库存
                    TripProductStore productStore = new TripProductStore();
                    productStore.setId(room.getStoreId());
                    tripProductStoreDao.deleteById(productStore);
                }else{
                    if(StringUtils.isEmpty(room.getId())){
                        room.setIsBook("0");
                        room.setIsCancel("0");
                        room.setCreationDate(new Date());
                        room.setSatisfaction("1");
                        tripHotelRoomDao.insert(room);//添加房间
                        //添加房间库存
                        TripProductStore productStore = new TripProductStore();
                        productStore.setProductId(room.getId());
                        productStore.setProductType("1");
                        productStore.setStore(room.getStore());
                        productStore.setCreationDate(new Date());
                        productStore.setModifyData(new Date());
                        tripProductStoreDao.insert(productStore);
                        //临时库存新增
                        TripHotelTempStore tripHotelTempStore = new TripHotelTempStore();
                        tripHotelTempStore.setHotelId(room.getHotelId());
                        tripHotelTempStore.setRoomId(room.getId());
                        tripHotelTempStore.setStore(room.getStore());
                        tripHotelTempStore.setRecordDate(new Date());
                        tripHotelTempStore.setCreationDate(new Date());
                        tripHotelTempStoreDao.insert(tripHotelTempStore);
                        num++;
                    }else{
                        room.setModifyDate(new Date());
                        tripHotelRoomDao.updateById(room);
                        //修改库存
                        TripProductStore productStore = new TripProductStore();
                        productStore.setId(room.getStoreId());
                        productStore.setStore(room.getStore());
                        tripProductStoreDao.updateById(productStore);
                        //临时库存新增
                        TripHotelTempStore tripHotelTempStore = new TripHotelTempStore();
                        tripHotelTempStore.setHotelId(room.getHotelId());
                        tripHotelTempStore.setRoomId(room.getId());
                        tripHotelTempStore.setStore(room.getStore());
                        tripHotelTempStore.setRecordDate(new Date());
                        tripHotelTempStore.setCreationDate(new Date());
                        tripHotelTempStoreDao.insert(tripHotelTempStore);
                        num++;
                    }
                }

            }
        }
        return num;
    }
}
